﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Infrastructure.Crosscutting.Comm.Service;

namespace Infrastructure.Crosscutting.Authorize
{
    public class UserService : IUserService
    {
        Domain.Uow.IRepository<Domain.Entity.User> userRepository;

        Domain.Uow.IRepository<Domain.Entity.UserExtension> userExtensionRepository;

        Domain.Uow.IRepository<Domain.Entity.UserRole> userRoleRepository;

        Domain.Uow.IRepository<Domain.Entity.Role> roleRepository;

        public UserService(Domain.Uow.IRepository<Domain.Entity.User> user,
             Domain.Uow.IRepository<Domain.Entity.UserExtension> userExtension,
             Domain.Uow.IRepository<Domain.Entity.UserRole> userRole,
             Domain.Uow.IRepository<Domain.Entity.Role> role)
        {
            if (user == null)
            {
                throw new ArgumentNullException("adminuserrepository is null");
            }
            if (userExtension == null)
            {
                throw new ArgumentNullException("adminUserExtensionRepository is null");
            }
            if (userRole == null)
            {
                throw new ArgumentNullException("adminUserIndividuationRepository is null");
            }
            if (role == null)
            {
                throw new ArgumentNullException("userActionRepository is null");
            }
            if (userRole == null)
            {
                throw new ArgumentNullException("userRoleRepository is null");
            }
            userRepository = user;
            userExtensionRepository = userExtension;
            userRoleRepository = userRole;
            roleRepository = role;
        }

        #region get

        /// <summary>
        /// Users the change PWD by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="pwd">The PWD.</param>
        /// <returns></returns>
        public bool UserChangePwdById(int id, string pwd, Domain.Entity.User operUser)
        {
            bool b = false;
            pwd = StringService.Tomd5(pwd);
            var user = userRepository.GetList(e => e.Id == id).FirstOrDefault();
            if (user != null && user.Id > 0)
            {
                user.Pwd = pwd;
                userRepository.Modify(user);
                userRepository.Uow.Commit();
                b = true;
                var log = LogCreateService.CreateLog();
                log.TypeId = (int)LogType.ModifySuccess;
                if (operUser != null)
                {
                    log.UserId = operUser.Id;
                    log.ShortMessage = "用户Id：" + operUser.Id + " 改变用户Id号：" + id.ToString() + " 的密码";
                    log.FullMessage = "UserChangePwdById 用户：" + operUser.UserName + " 用户Id：" + operUser.Id.ToString()
                        + " 改变用户Id号：" + id.ToString() + " 的密码：" + pwd;
                }
                else
                {
                    log.UserId = 0;
                    log.ShortMessage = "用户Id号：" + id.ToString() + " 的密码被修改";
                    log.FullMessage = "UserChangePwdById " + "用户Id号：" + id.ToString() + " 的密码被修改";
                }
            }
            return b;
        }

        /// <summary>
        /// Users the name of the change PWD by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="pwd">The PWD.</param>
        /// <returns></returns>
        public bool UserChangePwdByName(string name, string pwd, Domain.Entity.User operUser)
        {
            bool b = false;
            pwd = StringService.Tomd5(pwd);
            int id = GetUserIdByName(name);
            if (id > 0)
            {
                b = UserChangePwdById(id, pwd, operUser);
            }
            return b;
        }

        /// <summary>
        /// Gets the name of the user id by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public int GetUserIdByName(string name)
        {
            var id = 0;
            var res = userRepository.GetList(e => e.UserName == name).FirstOrDefault();
            if (res != null)
            {
                id = res.Id;
            }
            return id;
        }

        /// <summary>
        /// Gets the name of the user model by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public Domain.Entity.User GetUserModelByName(string name)
        {
            Domain.Entity.User user = null;
            user = userRepository.GetList(e => e.UserName == name).FirstOrDefault();
            return user;
        }

        /// <summary>
        /// Gets the user name by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public string GetUserNameById(int id)
        {
            var res = string.Empty;
            res = userRepository.GetList(d => d.Id == id).FirstOrDefault().UserName;
            return res;
        }

        /// <summary>
        /// Gets the name of the user model by.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public Domain.Entity.User GetUserModelById(int id)
        {
            return userRepository.GetList(d => d.Id == id).FirstOrDefault();
        }

        /// <summary>
        /// Gets the user model by user name and PWD.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="pwd">The PWD.</param>
        /// <returns></returns>
        public Domain.Entity.User GetUserModelByUserNameAndPwd(string name, string pwd)
        {
            string Pwd = StringService.Tomd5(pwd);
            return userRepository.GetList(e => e.UserName == name && e.Pwd == Pwd).FirstOrDefault();
        }

        /// <summary>
        /// Gets the user model by email.
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public Domain.Entity.User GetUserModelByEmail(string email)
        {
            return userRepository.GetList(d => d.Email == email).FirstOrDefault();
        }

        /// <summary>
        /// Gets the user model by QQ.
        /// </summary>
        /// <param name="qqId"></param>
        /// <returns></returns>
        public Domain.Entity.User GetUserModelByQQ(string qqId)
        {
            Domain.Entity.User user = null;
            var idModel = userExtensionRepository.GetList(d => d.QQNum == qqId).FirstOrDefault();
            if (idModel != null)
            {
                user = userRepository.GetList(d => d.Id == idModel.UserId).FirstOrDefault();
            }
            return user;
        }

        /// <summary>
        /// Gets the user model by sina.
        /// </summary>
        /// <param name="sinaId"></param>
        /// <returns></returns>
        public Domain.Entity.User GetUserModelBySina(string sinaId)
        {
            Domain.Entity.User user = null;
            var idModel = userExtensionRepository.GetList(d => d.SinaWeiboId == sinaId).FirstOrDefault();
            if (idModel != null)
            {
                user = userRepository.GetList(d => d.Id == idModel.UserId).FirstOrDefault();
            }
            return user;
        }

        /// <summary>
        /// Gets the user model by user GUID.
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <returns></returns>
        public Domain.Entity.User GetUserModelByUserGuid(string guid)
        {
            return userRepository.GetList(d => d.Guid == guid).FirstOrDefault();
        }

        /// <summary>
        /// Gets the user extension by user id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public Domain.Entity.UserExtension GetUserExtensionByUserId(int id)
        {
            return userExtensionRepository.GetList(d => d.UserId == id).FirstOrDefault();
        }

        public IQueryable<Domain.Entity.User> GetList()
        {
            var res = userRepository.GetList().AsQueryable();
            return res;
        }

        public Domain.Entity.User GetDefautlOperUser()
        {
            var user = userRepository.GetList(e => e.Id == 1).FirstOrDefault();
            return user;
        }

        #endregion


        #region add

        /// <summary>
        /// Regsters the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="userRole">The user role.</param>
        /// <param name="operUser">The oper user.</param>
        /// <returns></returns>
        public Domain.Entity.User Add(Domain.Entity.User user, Domain.Entity.UserRole userRole, Domain.Entity.User operUser)
        {
            if (user != null)
            {
                if (!ExistUserByName(user.UserName) && !ExistUserByEmail(user.Email))
                {
                    try
                    {
                        #region admin user

                        user.AddTime = System.DateTime.Now;
                        user.DepartmentID = 0;
                        //user.Email = 
                        user.Guid = GuidService.BuilderGuid();  //获得UserGuid
                        //user.Id = userRepository.GetList().Max(e => e.Id) + 1;
                        user.IpAddress = StringService.GetRealIP();
                        user.IsActivity = false;
                        user.Phone = "";
                        //user.Pwd = "";
                        user.Sex = true;
                        user.SortNum = 0;
                        user.State = true;
                        user.TrueName = user.UserName;
                        //user.UserType = "";
                        userRepository.Add(user);
                        #endregion

                        #region user role
                        userRole.UserId = user.Id;
                        userRoleRepository.Add(userRole);
                        #endregion

                        userRepository.Uow.Commit();

                        var log = LogCreateService.CreateLog();
                        log.TypeId = (int)LogType.AddSuccess;
                        if (operUser != null)
                        {
                            log.UserId = operUser.Id;
                            log.ShortMessage = "用户Id：" + operUser.Id + " 添加一个新用户Id：" + user.Id;
                            log.FullMessage = "AddUser 用户名：" + operUser.UserName + " 用户Id：" + operUser.Id
                                + " 添加用户Id：" + user.Id + " 添加用户名：" + user.UserName + " 添加用户角色Id：" + userRole.RoleId;
                        }
                        else
                        {
                            log.UserId = 0;
                            log.ShortMessage = "用户名：" + user.UserName + "被添加";
                            log.FullMessage = "AddUser " + "用户名：" + user.UserName + "被添加";
                        }
                        Infrastructure.Data.Uow.Log.LoggerRepositoryManager.AddLogInfo(log);
                    }
                    catch (Exception ex)
                    {
                        user.Id = 0;
                    }
                }
            }
            return user;
        }

        #endregion

        #region delete

        /// <summary>
        /// Deletes the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="operUser">The oper user.</param>
        /// <returns></returns>
        public bool Delete(Domain.Entity.User user, Domain.Entity.User operUser)
        {
            bool b = false;
            if (user != null && user.Id > 0)
            {
                user.State = false;
                var log = LogCreateService.CreateLog();
                log.TypeId = (int)LogType.DeleteSuccess;
                if (operUser != null)
                {
                    log.UserId = operUser.Id;
                    log.ShortMessage = "用户Id：" + operUser.Id.ToString() + " 非物理删除用户Id号：" + user.Id.ToString();
                    log.FullMessage = "DeleteUser 用户名：" + operUser.UserName + " 用户Id：" + operUser.Id.ToString()
                        + " 非物理删除用户Id号：" + user.Id.ToString();
                }
                else
                {
                    log.UserId = 0;
                    log.ShortMessage = "用户Id：" + user.Id.ToString() + " 被非物理删除";
                    log.FullMessage = "DeleteUser 用户名：" + user.UserName + " 用户Id：" + operUser.Id.ToString()
                        + " 被非物理删除";
                }
                try
                {
                    userRepository.Uow.Commit();
                    b = true;
                    Infrastructure.Data.Uow.Log.LoggerRepositoryManager.AddLogInfo(log);
                }
                catch (Exception ex)
                { }
            }
            return b;
        }

        public bool Delete(IList<int> userIdList, Domain.Entity.User operUser)
        {
            var deleteUserList = userRepository.GetList(e => userIdList.Contains(e.Id));
            foreach (var item in deleteUserList)
            {
                item.State = false;
                userRepository.Modify(item);
            }
            var res = false;
            try
            {
                userRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }

        public bool DeleteTrue(IList<int> userIdList, Domain.Entity.User operUser)
        {
            var deleteUserList = userRepository.GetList(e => userIdList.Contains(e.Id));
            foreach (var item in deleteUserList)
            {
                userRepository.Delete(item);
            }
            var res = false;
            try
            {
                userRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }

        /// <summary>
        /// Deletes the user true.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="operUser">The oper user.</param>
        /// <returns></returns>
        public bool DeleteTrue(Domain.Entity.User user, Domain.Entity.User operUser)
        {
            bool b = false;
            if (user != null && user.Id > 0)
            {
                try
                {
                    //要删除角色
                    var userRoles = userRoleRepository.GetList(d => d.UserId == user.Id);
                    foreach (var item in userRoles)
                    {
                        userRoleRepository.Delete(item);
                    }
                    userRepository.Delete(user);
                    //用户扩展信息
                    var userExtension = userExtensionRepository.GetList(d => d.UserId == user.Id);
                    foreach (var item in userExtension)
                    {
                        userExtensionRepository.Delete(item);
                    }

                    userRepository.Uow.Commit();
                    userRoleRepository.Uow.Commit();
                    userExtensionRepository.Uow.Commit();
                    var log = LogCreateService.CreateLog();
                    log.TypeId = (int)LogType.DeleteSuccess;
                    if (operUser != null)
                    {
                        log.UserId = operUser.Id;
                        log.ShortMessage = "用户Id：" + operUser.Id + " 物理删除用户Id号：" + user.Id.ToString();
                        log.FullMessage = "DeleteUserTrue 用户：" + operUser.UserName + " 用户Id：" + operUser.Id.ToString()
                            + " 删除用户Id号：" + user.Id.ToString();
                    }
                    else
                    {
                        log.UserId = 0;
                        log.ShortMessage = "用户Id号：" + user.Id.ToString() + " 被删除";
                        log.FullMessage = "DeleteUserTrue " + "用户Id号：" + user.Id.ToString() + " 被删除";
                    }
                    Infrastructure.Data.Uow.Log.LoggerRepositoryManager.AddLogInfo(log);
                    b = true;
                }
                catch (Exception ex)
                { }
            }
            return b;
        }
        /// <summary>
        /// Deletes the name of the user by.
        /// 只设置为不可用
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <param name="operUser">The oper user.</param>
        /// <returns></returns>
        public bool DeleteByUserGuid(string guid, Domain.Entity.User operUser)
        {
            var b = false;
            var user = userRepository.GetList(d => d.Guid == guid).FirstOrDefault();
            b = Delete(user, operUser);
            return b;
        }

        /// <summary>
        /// Deletes the name of the user true by.
        /// 物理删除
        /// </summary>
        /// <param name="guid">The GUID.</param>
        /// <param name="operUser">The oper user.</param>
        /// <returns></returns>
        public bool DeleteTrueByGuid(string guid, Domain.Entity.User operUser)
        {
            var b = false;
            var user = userRepository.GetList(d => d.Guid == guid).FirstOrDefault();
            b = DeleteTrue(user, operUser);
            return b;
        }

        /// <summary>
        /// Deletes the name of the user by.
        /// 只设置为不可用
        /// </summary>
        /// <param name="email"></param>
        /// <param name="operUser">The oper user.</param>
        /// <returns></returns>
        public bool DeleteByEmail(string email, Domain.Entity.User operUser)
        {
            var b = false;
            if (ExistUserByEmail(email))
            {
                var user = GetUserModelByEmail(email);
                b = Delete(user, operUser);
            }
            return b;
        }

        /// <summary>
        /// Deletes the name of the user true by.
        /// 物理删除
        /// </summary>
        /// <param name="email">The email.</param>
        /// <param name="operUser">The oper user.</param>
        /// <returns></returns>
        public bool DeleteTrueByEmail(string email, Domain.Entity.User operUser)
        {
            var b = false;
            if (!string.IsNullOrEmpty(email))
            {
                var user = GetUserModelByEmail(email);
                b = DeleteTrue(user, operUser);
            }
            return b;
        }

        /// <summary>
        /// Deletes the name of the user by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public bool DeleteByName(string name, Domain.Entity.User operUser)
        {
            bool b = false;
            var user = GetUserModelByName(name);
            b = Delete(user, operUser);
            return b;
        }

        /// <summary>
        /// Deletes the user by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public bool DeleteById(int id, Domain.Entity.User operUser)
        {
            bool b = false;
            var user = GetUserModelById(id);
            b = Delete(user, operUser);
            return b;
        }

        /// <summary>
        /// Deletes the user true by id.
        /// 物理删除
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="operUser"></param>
        /// <returns></returns>
        public bool DeleteTrueById(int id, Domain.Entity.User operUser)
        {
            var res = false;
            var user = GetUserModelById(id);
            res = DeleteTrue(user, operUser);
            return res;
        }

        /// <summary>
        /// Deletes the name of the user true by.
        /// 物理删除
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="operUser">The oper user.</param>
        /// <returns></returns>
        public bool DeleteTrueByName(string name, Domain.Entity.User operUser)
        {
            var res = false;
            var user = GetUserModelByName(name);
            res = DeleteTrue(user, operUser);
            return res;
        }

        #endregion

        #region modify

        /// <summary>
        /// Updates the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="operUser">The oper user.</param>
        /// <returns></returns>
        public bool Modify(Domain.Entity.User user, Domain.Entity.User operUser)
        {
            var b = false;
            if (user != null && user.Id > 0)
            {
                var log = LogCreateService.CreateLog();
                log.TypeId = (int)LogType.ModifySuccess;
                try
                {
                    userRepository.Modify(user);
                    userRepository.Uow.Commit();
                    b = true;
                    if (operUser != null)
                    {
                        log.UserId = operUser.Id;
                        log.ShortMessage = "用户Id：" + operUser.Id.ToString() + " 改变用户Id号：" + user.Id.ToString() + " 的信息";
                        log.FullMessage = "UpdateUser 用户名：" + operUser.UserName + " 用户Id：" + operUser.Id.ToString()
                            + " 改变了用户Id号：" + user.Id.ToString() + " 的信息";
                    }
                    else
                    {
                        log.UserId = 0;
                        log.ShortMessage = "用户Id号：" + user.Id.ToString() + " 的信息被修改";
                        log.FullMessage = "UpdateUser " + "用户Id号：" + user.Id.ToString() + " 的信息被修改";
                    }
                }
                catch (Exception ex)
                {
                    #region  exception
                    if (operUser != null && operUser.Id > 0)
                    {
                        log.ShortMessage = "更新用户数据异常";
                        log.FullMessage = log.ShortMessage = "更新用户Id：" + user.Id + " 用户名：" + user.UserName + " 操作用户Id：" + operUser.Id + " 错误信息：" + ex.Message;
                        if (ex.InnerException != null)
                        {
                            log.FullMessage += "   内部错误信息：" + ex.InnerException.Message;
                        }
                    }
                    else
                    {
                        log.ShortMessage = "更新用户数据异常";
                        log.FullMessage = log.ShortMessage = "更新用户Id：" + user.Id + " 用户名：" + user.UserName + " 错误信息：" + ex.Message;
                        if (ex.InnerException != null)
                        {
                            log.FullMessage += "   内部错误信息：" + ex.InnerException.Message;
                        }
                    }
                    #endregion
                }
                Infrastructure.Data.Uow.Log.LoggerRepositoryManager.AddLogInfo(log);
            }
            return b;
        }

        /// <summary>
        /// Updates the user extension.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="operUser">The oper user.</param>
        /// <returns></returns>
        public bool Modify(Domain.Entity.UserExtension user, Domain.Entity.User operUser)
        {
            var b = false;
            if (user != null && user.Id > 0 && user.UserId > 0)
            {
                var log = LogCreateService.CreateLog();
                log.TypeId = (int)LogType.ModifySuccess;
                try
                {
                    userExtensionRepository.Uow.Commit();
                    b = true;
                    if (operUser != null)
                    {
                        log.UserId = operUser.Id;
                        log.ShortMessage = "用户Id：" + operUser.Id + " 改变用户Id号：" + user.UserId.ToString() + " 的扩展信息";
                        log.FullMessage = "UpdateUser 用户名：" + operUser.UserName + " 用户Id：" + operUser.Id.ToString()
                            + " 改变了用户Id号：" + user.UserId.ToString() + " 的扩展信息";
                    }
                    else
                    {
                        log.UserId = 0;
                        log.ShortMessage = "用户Id号：" + user.UserId.ToString() + " 的扩展信息被修改";
                        log.FullMessage = "UpdateUser " + "用户Id号：" + user.UserId.ToString() + " 的扩展信息被修改";
                    }
                }
                catch (Exception ex)
                {
                    #region  exception
                    if (operUser != null && operUser.Id > 0)
                    {
                        log.ShortMessage = "更新用户数据异常";
                        log.FullMessage = log.ShortMessage = "更新用户Id：" + user.UserId.ToString() + " 操作用户Id：" + operUser.Id + " 错误信息：" + ex.Message;
                        if (ex.InnerException != null)
                        {
                            log.FullMessage += "   内部错误信息：" + ex.InnerException.Message;
                        }
                    }
                    else
                    {
                        log.ShortMessage = "更新用户数据异常";
                        log.FullMessage = log.ShortMessage = "更新用户Id：" + user.UserId.ToString() + " 错误信息：" + ex.Message;
                        if (ex.InnerException != null)
                        {
                            log.FullMessage += "   内部错误信息：" + ex.InnerException.Message;
                        }
                    }
                    #endregion
                }
                Infrastructure.Data.Uow.Log.LoggerRepositoryManager.AddLogInfo(log);
            }
            return b;
        }

        #endregion

        #region exist

        /// <summary>
        /// Exists the user by name PWD.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="pwd">The PWD.</param>
        /// <param name="isLogin">if set to <c>true</c> [is login].</param>
        /// <returns></returns>
        public bool ExistUserByNamePwd(string name, string pwd, bool isLogin)
        {
            bool b = false;
            pwd = Infrastructure.Crosscutting.Comm.Service.StringService.Tomd5(pwd);
            var user = userRepository.GetList(e => e.UserName == name)
                .Where(e => e.Pwd == pwd)
                .Where(d => d.IsActivity == true)
                .FirstOrDefault();
            if (user != null)
            {
                b = true;
                var log = LogCreateService.CreateLog();
                log.TypeId = (int)LogType.QuerySuccess;
                log.UserId = 0;
                log.FullMessage = "ExistUserByNamePwd 用户Id号：" + user.Id.ToString();
                log.ShortMessage = "查找用户名：" + name + ",成功";
                //查找成功
                if (isLogin)
                {
                    //添加日志用户日志
                    log.ShortMessage = "用户名：" + name + ",登陆成功,ExistUserByNamePwd";
                    log.TypeId = (int)LogType.LoginSuccess;
                }
                Infrastructure.Data.Uow.Log.LoggerRepositoryManager.AddLogInfo(log);
            }
            return b;
        }

        /// <summary>
        /// Exists the name of the user by.
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool ExistUserByEmail(string email)
        {
            return userRepository.GetList(d => d.Email == email).Any();
        }

        /// <summary>
        /// Exists the user by QQ.
        /// </summary>
        /// <param name="qqId">The qq id.</param>
        /// <returns></returns>
        public bool ExistUserByQQId(string qqId)
        {
            return userExtensionRepository.GetList(d => d.QQNum == qqId).Any();
        }

        /// <summary>
        /// Exists the user by sina.
        /// </summary>
        /// <param name="sinaId">The sina id.</param>
        /// <returns></returns>
        public bool ExistUserBySinaId(string sinaId)
        {
            return userExtensionRepository.GetList(d => d.QQWeiboId == sinaId).Any();
        }

        /// <summary>
        /// Exists the user by name PWD no MD5.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="pwd">The PWD.</param>
        /// <returns></returns>
        public bool ExistUserByNamePwdEncrypted(string name, string pwd)
        {
            bool b = false;
            var user = userRepository.GetList(e => e.UserName == name)
                .Where(e => e.Pwd == pwd)
                .Where(d => d.IsActivity == true)
                .FirstOrDefault();
            if (user != null)
            {
                b = true;
            }
            return b;
        }

        /// <summary>
        /// Exists the name of the user by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public bool ExistUserByName(string name)
        {
            bool b = false;
            int res = userRepository.GetList(e => e.UserName == name).Count();
            if (res > 0)
            {
                b = true;
            }
            return b;
        }

        #endregion

    }
}